package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.UserEmpowerConfig;
import com.ruoyi.web.service.IUserEmpowerConfigService;
import com.ruoyi.web.utils.IdWorker;
import com.ruoyi.web.vo.UserEmpowerConfigPageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 质押奖励配置Controller
 *
 * @author ruoyi
 * @date 2023-01-04
 */
@RestController
@RequestMapping("/empower/config")
public class UserEmpowerConfigController extends BaseController {
    @Autowired
    private IUserEmpowerConfigService userEmpowerConfigService;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询质押奖励配置列表
     */
    @PreAuthorize("@ss.hasPermi('web:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserEmpowerConfig userEmpowerConfig) {
        startPage();
        List<UserEmpowerConfig> list = userEmpowerConfigService.selectUserEmpowerConfigList(userEmpowerConfig);
        TableDataInfo info = getDataTable(list);

        List<UserEmpowerConfigPageVO> voList = new ArrayList<>();
        for (UserEmpowerConfig config : list) {
            UserEmpowerConfigPageVO vo = new UserEmpowerConfigPageVO();
            BeanUtils.copyProperties(config, vo);
            voList.add(vo);
        }
        return getDataTable(info, voList);
    }

    /**
     * 导出质押奖励配置列表
     */
    @PreAuthorize("@ss.hasPermi('web:config:export')")
    @Log(title = "质押奖励配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserEmpowerConfig userEmpowerConfig) {
        List<UserEmpowerConfig> list = userEmpowerConfigService.selectUserEmpowerConfigList(userEmpowerConfig);
        ExcelUtil<UserEmpowerConfig> util = new ExcelUtil<UserEmpowerConfig>(UserEmpowerConfig.class);
        util.exportExcel(response, list, "质押奖励配置数据");
    }

    /**
     * 获取质押奖励配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        UserEmpowerConfig config = userEmpowerConfigService.selectUserEmpowerConfigById(id);
        UserEmpowerConfigPageVO vo = new UserEmpowerConfigPageVO();
        BeanUtils.copyProperties(config, vo);
        return success(vo);
    }

    /**
     * 新增质押奖励配置
     */
    @PreAuthorize("@ss.hasPermi('web:config:add')")
    @Log(title = "质押奖励配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserEmpowerConfig userEmpowerConfig) {
        userEmpowerConfig.setId(idWorker.nextId());
        return toAjax(userEmpowerConfigService.insertUserEmpowerConfig(userEmpowerConfig));
    }

    /**
     * 修改质押奖励配置
     */
    @PreAuthorize("@ss.hasPermi('web:config:edit')")
    @Log(title = "质押奖励配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserEmpowerConfig userEmpowerConfig) {
        return toAjax(userEmpowerConfigService.updateUserEmpowerConfig(userEmpowerConfig));
    }

    /**
     * 删除质押奖励配置
     */
    @PreAuthorize("@ss.hasPermi('web:config:remove')")
    @Log(title = "质押奖励配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userEmpowerConfigService.deleteUserEmpowerConfigByIds(ids));
    }
}
